package com.siyuanren.gcocp.web;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.siyuanren.gcocp.common.config.PublicConfig;
import com.siyuanren.gcocp.common.web.bean.Result;
import com.siyuanren.gcocp.dto.ActiveInviteDTO;
import com.siyuanren.gcocp.dto.wx.WxUserVoucherDTO;
import com.siyuanren.gcocp.entity.WxActive;
import com.siyuanren.gcocp.entity.WxCourse;
import com.siyuanren.gcocp.service.ActiveService;
import com.siyuanren.gcocp.util.VaildUtil;
import com.siyuanren.gcocp.vo.QueryActiveVO;
import com.siyuanren.gcocp.vo.QueryActiveVoucherVO;
import com.siyuanren.gcocp.vo.SendVoucherVO;
import com.siyuanren.gcocp.vo.StartActiveVO;
import com.siyuanren.gcocp.web.wx.BaseWxController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * Created by   anxing
 * Project_name gcocp-parent
 * Path         com.siyuanren.gcocp.web
 * Date         2018/6/12
 * Time         15:47
 * Description
 */
@Slf4j
@Controller
@RequestMapping("active")
public class ActiveController extends BaseWxController {
    @Autowired
    private ActiveService activeService;

    /**
     * 活动邀请页面
     */
    @RequestMapping("detail")
    @ResponseBody
    public Result activeDetail(Model model, QueryActiveVO activeVo) throws Exception {
        VaildUtil.check(activeVo);
        activeVo.setUserId(getUserId());

        ActiveInviteDTO activeInviteDTO = activeService.getActiveDetail(activeVo);
        return new Result(activeInviteDTO);
    }

    /**
     * 参加活动
     * @return
     */
    @RequestMapping("startActive")
    @ResponseBody
    public Result startActive(StartActiveVO vo) throws Exception {
        VaildUtil.check(vo);
        vo.setUserId(getUserId());
        activeService.startActive(vo);
        return new Result();
    }

    /**
     * 通过扫描优惠券二维码, 发送优惠券
     * 领取优惠券成功后跳转到课程详情页面购买
     *
     * @return
     */
    @RequestMapping("sendVoucher")
    public String sendVoucher(SendVoucherVO vo) {
        vo.setUserId(getUserId());
        // 如果优惠券不为空,则发送优惠券, 如果优惠券为空, 则直接打开对应的课程详情.
        if (vo.getVoucherId() != null && vo.getVoucherId() != 0) {
            activeService.sendVoucher(vo);
        }
        return "redirect:"+ PublicConfig.PUBLIC_WX_URL + "/#/course/CourseDetail/" + vo.getCourseId();
    }
}
